home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1611 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  91 lines

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to initialize an array of structures?
  5. Date: 11 Jan 1996 19:45:17 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Message-ID: <4d3pcd$uj@clarknet.clark.net>
  8. References: <4d240e$nk5@jupiter.planet.net> <4d358f$kj9@tahko.lpr.carel.fi>
  9. NNTP-Posting-Host: explorer.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  14.  
  15. Ari Lukumies (aril@cmt.lpr.mail.carel.fi) wrote:
  16. : Chris Kemp <chrisk@paladn.com> wrote:
  17. : >Could someone explain how to do this properly:
  18. : >        struct FUNCTIONMAP
  19. : >        {
  20. : >            char     *functionname;
  21. : >            int    functionnumber;
  22. : >            int    maxargs;
  23. : >            int    functarg[5];
  24. : >            
  25. : >        };
  26. : >        
  27. : >        static struct FUNCTIONMAP functionlist[300];    
  28. : >        functionlist[1]={"firstfunction",1,3,2,2,2,0,0}    <-- hangs on this line
  29. : >        
  30. : >TIA            
  31. : You would want to use braces:
  32. :     static struct FUNCTIONMAP    functionlist[300];
  33. :     functionlist[0] = { "firstfunction", 1, 3, { 2, 2, 2, 0, 0 } };
  34.  
  35. !!!!! NO !!!!!
  36.  
  37. The braces {} convention only works as an initializer (that is, as part 
  38. of the same statement that contains the declaration). It cannot be used to 
  39. assign to an existing object. And even as an initializer, it cannot be 
  40. used to initialize arbitrary elements of the array. It is used to fill 
  41. the array beginning at the first element, until the initializer list is 
  42. exhausted.
  43.  
  44. The only ways to set up a convenient assignment like this are to create 
  45. an assign member function: 
  46.  
  47.         FUNCTIONMAP& assign (const char *fname, 
  48.                     int num,
  49.                     int maxa,
  50.                     int arg1, int arg2, int arg3,
  51.                     int arg4, int arg5) {
  52.             functionname = new char[strlen(fname)];
  53.             strcpy(functionname, fname);
  54.             functionnumber = num;
  55.             [... and so forth ...]
  56.         }
  57.  
  58. or a non-empty constructor in addition to an empty constructor: 
  59.  
  60.     public:
  61.         FUNCTIONMAP() {}
  62.         FUNCTIONMAP(const char *fname, 
  63.                 int num,
  64.                 int maxa,
  65.                 int arg1, int arg2, int arg3,
  66.                 int arg4, int arg5) {
  67.             functionname = new char[strlen(fname)];
  68.             strcpy(functionname, fname);
  69.             functionnumber = num;
  70.             [... and so forth ...]
  71.         }
  72.  
  73. Then you could use
  74.  
  75.     functionlist[1].assign("firstfunction", 1, 3, 2, 2, 2, 0, 0)
  76.  
  77. or
  78.  
  79.     functionlist[1] = FUNCTIONMAP("firstfunction", 1, 3, 2, 2, 2, 0, 0)
  80.  
  81. respectively.
  82.  
  83. In the second case, you might want to overload the assignment operator as 
  84. well.
  85.  
  86.